/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.uicomponents;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
 * UI component for displaying User-comments for places
 * It basically consists of a picture on the right and the
 * comment text which is wrapped on the left side.
 *
 */
public class CommentUI extends CustomItem {
	private String title;
	private String text;
	private Image picture;
	private int width = 50;
	private int height = 20;
	
	private Font font;
	private final int BORDER_OFFSET = 2;

	/**
	 * Constructor
	 * @param title Title
	 * @param commentText Text to be displayed on the component
	 * @param picture Image displayed on the component
	 * @param width Width of the component
	 */
	public CommentUI(String title, String commentText, Image picture, int width){
		super(null);
		this.title = title;
		this.text = commentText;
		this.picture = picture;
		this.width = width;
		this.font = Font.getDefaultFont();
	}
	
	protected int getMinContentHeight() {
		int freeSpace = getMinContentWidth()-this.picture.getWidth()+(BORDER_OFFSET*2);
		this.height = matchTextIntoUI(title, text, freeSpace, null , false);
		if(this.height > picture.getHeight()){
			return this.height + BORDER_OFFSET*2;
		}else
			return this.picture.getHeight() + BORDER_OFFSET*2;
	}

	protected int getMinContentWidth() {
		return this.width-BORDER_OFFSET*4;
	}

	protected int getPrefContentHeight(int arg0) {
		return getMinContentHeight();
	}

	protected int getPrefContentWidth(int arg0) {
		return getMinContentWidth();
	}

	protected void paint(Graphics g, int w, int h) {
		g.drawRect(0, 0, w-1, h-1);
		int size = getMinContentWidth()-this.picture.getWidth()+(BORDER_OFFSET*2);
		g.drawImage(picture, getMinimumWidth()-BORDER_OFFSET, BORDER_OFFSET, Graphics.TOP | Graphics.RIGHT);
		matchTextIntoUI(title, text, size, g, true);
	}
	
	/**
	 * Does the wrapping of the text inside on the component. It has to wrap the text such that
	 * it doesn't overlap with the picture
	 * @param title
	 * @param message
	 * @param maxLength
	 * @param g
	 * @param draw true if text should already be drawn, false if just for measuring purposes
	 * @return int representing the needed height of the displayed text
	 */
	private int matchTextIntoUI(String title, String message, int maxLength, Graphics g, boolean draw){
		int textHeight = font.getHeight();
		int lineSpace = 0;
		
		Font boldFont=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
		if(draw){
			g.setFont(boldFont);
			if(boldFont.stringWidth(title) > maxLength){
				g.drawString(title.substring(0,title.length()*50/100) + "...", BORDER_OFFSET, textHeight, Graphics.BASELINE | Graphics.LEFT);
			}else
				g.drawString(title, BORDER_OFFSET, textHeight, Graphics.BASELINE | Graphics.LEFT);
			g.setFont(font);
		}
		textHeight += boldFont.getHeight() + lineSpace;
		
		int lastPossibleSplit = 0;
		int lastSplitted = 0;
		for(int i=0; i<message.length(); i++){
			if(message.charAt(i) == ' ' || (i+1) == message.length()){
				String sub = message.substring(lastSplitted,i);
				int substringWidth = font.stringWidth(sub);
				if(substringWidth > maxLength){
					sub = message.substring(lastSplitted, lastPossibleSplit).trim();
					if(draw) g.drawString(sub, BORDER_OFFSET, textHeight, Graphics.BASELINE | Graphics.LEFT);
					lastSplitted = lastPossibleSplit;
					textHeight += font.getHeight() + lineSpace;
				}
				lastPossibleSplit = i;
			}
		}
		if(lastSplitted != message.length()){
			if(draw) g.drawString(message.substring(lastSplitted,message.length()).trim(), BORDER_OFFSET, textHeight, Graphics.BASELINE | Graphics.LEFT);
		}
		
		return textHeight;
	}

	
	/*
	 * Getters and Setters 
	 */
	
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}
}
